package com.chennan.book.api.rest.servicecontroller;

import com.chennan.book.api.rest.servicecontroller.dto.ExecutePlan;
import com.chennan.book.domain.service.RunExecuteTask;
import com.chennan.javabase.domain.juc.forkjoin.ForkJoinDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @author chennan
 * @create 2022-02
 */
@RestController
public class BatchThreadPool {
    @Autowired
    @Qualifier("getThreadPoolExecutor")
    private ExecutorService executorService;



    @GetMapping("/start")
    public String getSubmit() {
        for (int i = 0; i < 1000; i++) {
            RunExecuteTask runExecuteTask = new RunExecuteTask("任务执行"+i);
            executorService.execute(runExecuteTask);
        }
        return "执行中";
    }
    @GetMapping("/checkExecute")
    public ExecutePlan checkExecute() throws ExecutionException, InterruptedException {
        ExecutePlan executePlan = new ExecutePlan();
        executePlan.setSumThreadCount(Thread.activeCount());
        executePlan.setCurrntCount(RunExecuteTask.atomicLong.get());
        executePlan.setSumCount(1000000L);
        return executePlan;

//        return "已经执行百分比:"+String.valueOf((ForkJoinDemo.atomicLong.get()/10001.00)*100)+"%"+
//                "/n,正在执行任务线程数："+forkJoinPool.getRunningThreadCount()+
//                "/n,getActiveThreadCount："+forkJoinPool.getActiveThreadCount()
//                ;
    }
}
